package com.fei.controller;

import com.fei.bean.FileAudioBean;
import com.fei.bean.MusicBean;
import com.fei.bean.SingerBean;
import com.fei.bean.UserBean;
import com.fei.service.FileAudioService;
import com.fei.service.MusicService;
import com.fei.service.SingerService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class MusicController {

    @Autowired
    MusicService musicService;
    @Autowired
    FileAudioService fileAudioService;
    @Autowired
    SingerService singerService;

//    首页
    @RequestMapping("/user/index")
    public String index(Model model, HttpSession session){

        //用户登录的数据
        Subject subject = SecurityUtils.getSubject();
        UserBean user = (UserBean) subject.getSession().getAttribute("user");

        List<MusicBean> musicBeanList = musicService.selectMusicEightService();
        //用户的播放列表
        List<FileAudioBean> audio = fileAudioService.selectUserBroadcast(user.getPhone());
//        判断这些中是否已经有被用户添加到歌单过的
        List<MusicBean> musicBeans = UserSong_list(musicBeanList, audio);

        session.setAttribute("FileAudioBeanList",audio);
        model.addAttribute("userBeanTopAndButton",user);
        model.addAttribute("musicBeanList",musicBeans);

        model.addAttribute("panduan",1);
        return "commons/home";
    }

//    首页2，局部更新版
    @RequestMapping("/user/ajax/index")
    public String index2(Model model, HttpSession session){

        //用户登录的数据
        Subject subject = SecurityUtils.getSubject();
        UserBean user = (UserBean) subject.getSession().getAttribute("user");

        List<MusicBean> musicBeanList = musicService.selectMusicEightService();
        //用户的播放列表
        List<FileAudioBean> audio = fileAudioService.selectUserBroadcast(user.getPhone());
//        判断这些中是否已经有被用户添加到歌单过的
        List<MusicBean> musicBeans = UserSong_list(musicBeanList, audio);

        session.setAttribute("FileAudioBeanList",audio);
        model.addAttribute("userBeanTopAndButton",user);
        model.addAttribute("musicBeanList",musicBeans);

        model.addAttribute("panduan",0);
        return "index::index";
    }


//    listen页面
    @RequestMapping("/user/ajax/listenAll")
    public String tolisten(Model model,Integer id,HttpSession session){
        List<MusicBean> musicBeanList = musicService.findAllMusicService();
        if (id == null){
            id = singerService.selectOneSingerBeanService();
        }
        if(id != null){
            SingerBean singerBean = singerService.selectSingerIdAndMusicService(id);
            UserBean user = (UserBean) session.getAttribute("user");
            List<FileAudioBean> audio = fileAudioService.selectUserBroadcast(user.getPhone());
//            判断用户是否添加进歌单中
            List<MusicBean> musicBean = UserSong_list(singerBean.getMusicBean(), audio);
            singerBean.setMusicBean(musicBean);
            model.addAttribute("singerBean",singerBean);
        }
        model.addAttribute("musicBeanList",musicBeanList);
        return "listen::listen";
    }

    @RequestMapping("/user/ajax/listen")
    public String toListenLeft(Model model,Integer id,HttpSession session){
        System.out.println(id);
        SingerBean singerBean = singerService.selectSingerIdAndMusicService(id);
        UserBean user = (UserBean) session.getAttribute("user");
        List<FileAudioBean> audio = fileAudioService.selectUserBroadcast(user.getPhone());
//            判断用户是否添加进歌单中
        List<MusicBean> musicBean = UserSong_list(singerBean.getMusicBean(), audio);
        singerBean.setMusicBean(musicBean);
        model.addAttribute("singerBean",singerBean);
        return "commons/commons::listenLeft";
    }













//    判断用户是否有添加到歌单中，有在musicBean中的userBroadcast返回ture
//    用的话在首页中显示已经添加的图标
    public List<MusicBean> UserSong_list(List<MusicBean> musicBeanList,List<FileAudioBean> audio){
        if(musicBeanList != null){
            if(audio != null){
                for (int i = 0;i<audio.size();i++){
                    for(int j = 0;j<musicBeanList.size();j++){
                        if(audio.get(i).getTitle().equals(musicBeanList.get(j).getSong_title())){
                            musicBeanList.get(j).setUserBroadcast(true);
                            break;
                        }
                    }
                }
            }
        }
        return musicBeanList;
    }



}
